package com.dingreading.cloud.subject.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.dto.LoginDataDto;
import com.dingreading.cloud.common.util.HttpUtils;
import com.dingreading.cloud.common.util.RedisParam;
import com.dingreading.cloud.subject.entity.Stores;
import com.dingreading.cloud.subject.service.StoresService;
import com.dingreading.cloud.subject.util.LoginInfoUtil;
import com.dingreading.cloud.subject.util.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/front/hsyd")
@Api(tags = "门店-绘说有道2.0管理")
public class FrontHsydController {

    @Resource
    private StoresService storesService;
    @Resource
    private RedisService redisService;


    @ApiOperation(value = "学科列表")
    @PostMapping(value = "/subjectList")
    public R<Object> subjectList(
            HttpServletRequest request,
            @ApiParam(value = "学科id") @RequestParam(value = "id") Long id
    ) {
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();

        String currentStoreUid = loginDto.getCurrentStoreUid();
        Stores stores = storesService.getByCorpUidAndStoreUid(null, currentStoreUid);
        if (!stores.getCooperationType().contains("26"))
            return R.fail("门店没有此合作权限");

        String redisKey = RedisParam.HSYD_SUBJECT;
        String json = (String) redisService.get(redisKey);

        JSONArray treeJson = new JSONArray();
        if (StringUtils.isNotBlank(json)) {
            // 获取redis缓存数据
            treeJson = JSONArray.parseArray(json);
        } else {
            String url = "https://lucky.xiaoyuxiang.com:8000/castle-provider-teaching/front/data/dataSubject/dingSubjectList";
            Map<String, String> map = new HashMap<>();
            map.put("id", id + "");

            String requestResult = HttpUtils.post(url, map);
            if (StringUtils.isNotBlank(requestResult)) {
                JSONObject jsonObject = JSONObject.parseObject(requestResult);
                Integer status = jsonObject.getInteger("status");
                if (status != null && status.equals(200)) {
                    treeJson = jsonObject.getJSONArray("data");

                    // 缓存数据
                    redisService.set(redisKey, treeJson.toJSONString());
                }
            }
        }
        return R.ok(treeJson);
    }


    @ApiOperation(value = "课次列表")
    @PostMapping(value = "/lessonList")
    public R<Object> lessonList(
            @ApiParam(value = "学科id") @RequestParam(value = "submainId") Long submainId,
            @ApiParam(value = "学期id") @RequestParam(value = "subjectId") Long subjectId
    ) {
        String redisKey = RedisParam.HSYD_LESSON + subjectId;
        String json = (String) redisService.get(redisKey);

        JSONArray array = new JSONArray();
        if (StringUtils.isNotBlank(json)) {
            // 获取redis缓存数据
            array = JSONArray.parseArray(json);
        } else {
            String url = "https://lucky.xiaoyuxiang.com:8000/castle-provider-teaching/front/data/dataLesson/dingLessonList";
            Map<String, String> map = new HashMap<>();
            map.put("submainId", submainId + "");
            map.put("subjectId", subjectId + "");

            String requestResult = HttpUtils.post(url, map);
            if (StringUtils.isNotBlank(requestResult)) {
                JSONObject jsonObject = JSONObject.parseObject(requestResult);
                Integer status = jsonObject.getInteger("status");
                if (status != null && status.equals(200)) {
                    array = jsonObject.getJSONArray("data");

                    // 缓存数据
                    redisService.set(redisKey, array.toJSONString());
                }
            }
        }
        return R.ok(array);
    }


    @ApiOperation(value = "课次详情")
    @PostMapping(value = "/lesson")
    public R<Object> lesson(
            @ApiParam(value = "课次id") @RequestParam(value = "id") Long id
    ) {
        String url = "https://lucky.xiaoyuxiang.com:8000/castle-provider-teaching/front/data/dataLesson/dingLesson";
        Map<String, String> map = new HashMap<>();
        map.put("id", id + "");

        String requestResult = HttpUtils.post(url, map);
        if (StringUtils.isNotBlank(requestResult)) {
            JSONObject jsonObject = JSONObject.parseObject(requestResult);
            Integer status = jsonObject.getInteger("status");
            if (status != null && status.equals(200)) {
                JSONObject data = jsonObject.getJSONObject("data");
                return R.ok(data);
            }
        }
        return R.fail("查询失败");
    }

}